<?php
/**
 * lertaLight - A smart ContentManagementSystem
 * --
 * 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * --
 *
 * @package 	lertaLight
 * @author 		Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @copyright 	2010 Frederic Darmstädter <fredericdarmstaedter@gmail.com>
 * @license 	http://opensource.org/licenses/mit-license.php MIT License
 * @version 	2.0 (2010-01-16)
 */
	
/* CONTENT OF FILES */
$create = new create();

$create->folder('readyToShip');

$installfilename = 'readyToShip/installer.php';
$installcodename = 'readyToShip/code.ll';
$codename = 'code.ll';

$file_installer = '<?php
	/**
	 * lertaLight
	 * 
	 * (c) 2010 by Frederic Darmstädter
	 *
	 * This file creates a complete lertaLight installation on your Server.
	 *
	 */
	$create = new create();
	';
$create->file($installfilename,$file_installer);
generate('.',$installfilename, $installcodename);
function generate($folder,$installfilename,$installcodename){
	// Get Content of the Files
	$create = new create();
	$handler = opendir($folder);
	while ($file = readdir($handler)) {
		if ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != '.svn' && $file != 'createinstallfile.php'){
			if(is_dir($folder.'/'.$file)){
				$file_installer = '$create->folder(\''.$folder.'/'.$file.'\');';
				$create->file($installfilename,$file_installer);
				generate($folder.'/'.$file,$installfilename,$installcodename);			
			}
			else{
				$filecontent = fopen($folder.'/'.$file, 'r');
				$filecontentcontent = file_get_contents($folder.'/'.$file);
				fclose($filecontent);
				$create->file($installcodename, '####################'.$folder.'/'.$file.'####################');
				$create->file($installcodename,$filecontentcontent);			
			}
		}
	}
	closedir($handler);
}
	
$file_installer =	'

	// Create files
	$codeFile = fopen(\''.$codename.'\', \'r\');
	$fileContent = file_get_contents(\''.$codename.'\');
	fclose($codeFile);
	$codeParts = explode("####################", $fileContent);
	$partSize = count($codeParts);
	$i = 1;
	  while($i < ($partSize/2))
	    {
			if (! ($i % 2 == 0)) 
			{ 
				$create->file($codeParts[$i],$codeParts[$i+1]);
			}
	    
		$i++;
	    }
	
		// Delete Tempfiles/folders
		unlink(\'code.ll\');
		unlink(\'readyToShip/code.ll\');
		rmdir(\'readyToShip\');
		unlink(\'installer.php\');
		
		
		$file = fopen(\'lertaLight/conf/configuration.yaml\', \'r\');
		$content = file_get_contents(\'lertaLight/conf/configuration.yaml\');
		fclose($file);

		$newBaseUrl = str_replace(\'installer.php\', \'\', $_SERVER[\'REQUEST_URI\']);
		$content = str_replace(\'baseUrl: /\', \'baseUrl: \'.$newBaseUrl, $content);

		$handle = fopen(\'lertaLight/conf/configuration.yaml\',\'w\');
		fwrite($handle,$content);
		fclose($handle);
		
		
		
		header(\'location: \' . $newBaseUrl . \'index.html\');

	class create{

		function folder($name){
			if(! is_dir($name)){
				mkdir($name, 0777);
			}
		}

		function file($filename, $content){
			if(! file_exists($filename)){
				$handle = fopen($filename,\'w\');
				fwrite($handle,$content);
				fclose($handle);
			}
		}
	}
	

';
$create->file($installfilename,$file_installer);
print 'Installationsdatei wurde generiert!';


class create{
	
	function folder($name){
		if(! is_dir($name)){
			mkdir($name, 0777);
		}
	}
	
	function file($filename, $content){
			$handle = fopen($filename,"a");
			fwrite($handle,$content);
			fclose($handle);
	}
}